﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
using Pay.Token.Utils;
using System.Threading.Tasks;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 活动商品
    /// </summary>
    [AppAuthorize("Member")]
    public class ProductActivityController : GeneralController {
        private IProductService _iProductService;
        private IMerchantService _iMerchantService;
        private IAdvertisementService _iAdvertisementService;
        private IProductActivityService _iProductActivityService;
        private IActivityService _iActivityService;

        public ProductActivityController(IProductService iProductService, IMerchantService iMerchantService, IAdvertisementService iAdvertisementService,
                                         IProductActivityService iProductActivityService, IActivityService iActivityService) {
            _iProductService = iProductService;
            _iMerchantService = iMerchantService;
            _iAdvertisementService = iAdvertisementService;
            _iProductActivityService = iProductActivityService;
            _iActivityService = iActivityService;
        }

        #region 限时购
        [HttpGet, AllowAnonymous]
        public ActionResult ProductTimeBuy(long activityId = 0) {
            var activity = _iActivityService.GetActivity(activityId);
            if (activity == null || activity.end_time <= DateTime.Now) {//限时购活动已结束，跳转至结束页
                return View("ProductTimeBuyEnd");
            }
            var banner = _iAdvertisementService.AppGetAdvertisementList(new merch_advertisement_query_type() { position_type = 10, is_type = false });
            ViewBag.banner = banner;
            ViewBag.img_url = RequestMethod.token_imgurl;

            return View(activity);
        }

        [HttpPost, AllowAnonymous]
        public PartialViewResult _ProductTimeBuyList(ProductActivityQuery query) {
            ProductActivityQuery activityQuery = new ProductActivityQuery();
            activityQuery.ActivityType = EActivityType.ProductTimeBuy;
            activityQuery.activityId = query.activityId;
            activityQuery.Time = DateTime.Now;
            activityQuery.page = query.page;
            activityQuery.size = query.size;
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            activityQuery.merchant_id = merchant.id;
            var list = _iProductActivityService.Gets(activityQuery);
            int userType = 3;
            if (UserId > 0) {
                var curUser = _iMerchantService.Get(UserId);
                userType = (int)curUser.usertype;
            }
            ViewBag.UserType = userType;
            ViewBag.UserId = UserId;
            return PartialView(list);
        }
        #endregion

        #region 红钻+现金
        [HttpGet, AllowAnonymous]
        public ActionResult ProductCashRedDrill(long activityId = 0) {
            var activity = _iActivityService.GetActivity(activityId);
            if (activity == null || activity.end_time <= DateTime.Now) {//限时购活动已结束，跳转至结束页
                return View("ActivityEnd");
            }

            ViewBag.img_url = RequestMethod.token_imgurl;
            return View(activity);
        }
        [HttpPost, AllowAnonymous]
        public PartialViewResult _ProductCashRedDrill(ProductActivityQuery query) {
            ProductActivityQuery activityQuery = new ProductActivityQuery();
            activityQuery.ActivityType = EActivityType.ProductCashRedDrill;
            activityQuery.activityId = query.activityId;
            activityQuery.Time = DateTime.Now;
            activityQuery.page = query.page;
            activityQuery.size = query.size;
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            activityQuery.merchant_id = merchant.id;
            var list = _iProductActivityService.Gets(activityQuery);
            int userType = 3;
            if (UserId > 0) {
                var curUser = _iMerchantService.Get(UserId);
                userType = (int)curUser.usertype;
            }
            ViewBag.UserType = userType;
            ViewBag.UserId = UserId;
            return PartialView(list);
        }
        #endregion

        #region 双旦活动
        [HttpGet, AllowAnonymous]
        public ActionResult Christmas() {
            ProductActivityQuery activityQuery = new ProductActivityQuery();
            activityQuery.ActivityType = EActivityType.ProductTimeBuy;
            activityQuery.Time = DateTime.Now;
            activityQuery.page = 1;
            activityQuery.size = 50;
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            activityQuery.merchant_id = merchant.id;

#if DEBUG
            var activity = _iActivityService.GetActivity(50);
            ViewBag.activity = activity;
            activityQuery.activityId = 50;
            var list1 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list1 = list1.rows ?? new List<product_dto>();


            activityQuery.activityId = 51;
            var list2 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list2 = list2.rows ?? new List<product_dto>();


            activityQuery.activityId = 52;
            var list3 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list3 = list3.rows ?? new List<product_dto>();


            activityQuery.activityId = 53;
            var list4 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list4 = list4.rows ?? new List<product_dto>();


            activityQuery.activityId = 54;
            var list5 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list5 = list5.rows ?? new List<product_dto>();
#else
            var activity = _iActivityService.GetActivity(39);
            ViewBag.activity = activity;
            activityQuery.activityId = 39;
            var list1 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list1 = list1.rows ?? new List<product_dto>();


            activityQuery.activityId = 40;
            var list2 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list2 = list2.rows ?? new List<product_dto>();


            activityQuery.activityId = 41;
            var list3 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list3 = list3.rows ?? new List<product_dto>();


            activityQuery.activityId = 42;
            var list4 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list4 = list4.rows ?? new List<product_dto>();


            activityQuery.activityId = 43;
            var list5 = _iProductActivityService.Gets(activityQuery);
            ViewBag.list5 = list5.rows ?? new List<product_dto>();
#endif
            return View();
        }
        #endregion


    }
}